//
//  RemoteControlViewController.swift
//  MyVideo
//
//  Created by han on 2021/4/22.
//

import UIKit
import AVFoundation

class RemoteControlViewController: UIViewController {
    var player: AVPlayer?
    
    @IBOutlet weak var skipBack30Button: UIButton!
    @IBOutlet weak var playPauseButton: UIButton!
    @IBOutlet weak var skipAhead30Button: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        playPauseButton.isSelected = (player?.rate != 0)
    }
    
    @IBAction func playPauseButtonPressed(_ sender: UIButton) {
        playPauseButton.isSelected = !playPauseButton.isSelected
        playPauseButton.isSelected ? player?.play() : player?.pause()
    }
    
    @IBAction func skipBack30ButtonPressed(_ sender: UIButton) {
        guard let duration = player?.currentItem?.duration else { return }
        let targetTime = max(player!.currentTime() - CMTime(seconds: 30, preferredTimescale: duration.timescale), .zero)
        player?.seek(to: targetTime)
    }
    
    @IBAction func skipAhead30ButtonPressed(_ sender: UIButton) {
        guard let duration = player?.currentItem?.duration else { return }
        let targetTime = min(duration, player!.currentTime() + CMTime(seconds: 30, preferredTimescale: duration.timescale))
        player?.seek(to: targetTime)
    }
    
}
